import { defineStore } from 'pinia';
import { store } from './../index';
interface userState {
  menus: adminMenusResType[];
  username: string;
  adminList: IAdminListResArr[];
}

export const useUserStore = defineStore('user', {
  state(): userState {
    return {
      menus: [],
      username: '',
      adminList: [],
    };
  },
  getters: {
    getMenusConf(state) {
      const newMenus: { [key: number]: adminMenusResType } = {};
      state.menus.forEach((menu) => {
        if (menu.parentId == 0) {
          newMenus[menu.id] = { ...menu, list: newMenus[menu.id]?.list };
        } else {
          if (menu.hidden === 1) return;
          newMenus[menu.parentId] ??= {} as adminMenusResType;
          newMenus[menu.parentId].list ??= [];
          newMenus[menu.parentId].list?.push(menu);
        }
      });
      return newMenus;
    },
  },
  actions: {
    updataMenus(menus: adminMenusResType[]) {
      this.menus = menus;
    },
    // updateAdminList(adminList: IAdminListResArr[]) {
    //   this.adminList = adminList;
    // },
    updateUsername(username: string) {
      this.username = username;
    },
  },
});
export const useUserStoreWithout = () => useUserStore(store);
